package se.jocke.nb.proxy.ui;

import javax.swing.JSpinner;
import javax.swing.JTextField;
import org.openide.util.Lookup;
import se.jocke.nb.proxy.monitor.service.ProxySettings;
import se.jocke.nb.proxy.monitor.service.ProxySettingsManager;

final class ProxyMonitorPanel extends javax.swing.JPanel {

    private final ProxyMonitorOptionsPanelController controller;

    ProxyMonitorPanel(ProxyMonitorOptionsPanelController controller) {
        this.controller = controller;
        initComponents();
        JTextField tf1 = ((JSpinner.NumberEditor) portSpinner.getEditor()).getTextField();
        tf1.setHorizontalAlignment(JTextField.LEFT);
        tf1.setEditable(false);

        JTextField tf2 = ((JSpinner.NumberEditor) proxyToPortSpinner.getEditor()).getTextField();
        tf2.setHorizontalAlignment(JTextField.LEFT);
        tf2.setEditable(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        portSpinner = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();
        proxyToPortSpinner = new javax.swing.JSpinner();
        jLabel3 = new javax.swing.JLabel();
        proxyToHostTextField = new javax.swing.JTextField();

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(ProxyMonitorPanel.class, "ProxyMonitorPanel.jLabel1.text")); // NOI18N

        portSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(48080), null, null, Integer.valueOf(1)));
        portSpinner.setEditor(new javax.swing.JSpinner.NumberEditor(portSpinner, " 0000"));
        portSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                portSpinnerStateChanged(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(ProxyMonitorPanel.class, "ProxyMonitorPanel.jLabel2.text")); // NOI18N

        proxyToPortSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(8080), null, null, Integer.valueOf(1)));
        proxyToPortSpinner.setEditor(new javax.swing.JSpinner.NumberEditor(proxyToPortSpinner, "0000"));
        proxyToPortSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                proxyToPortSpinnerStateChanged(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(ProxyMonitorPanel.class, "ProxyMonitorPanel.jLabel3.text")); // NOI18N

        proxyToHostTextField.setText(org.openide.util.NbBundle.getMessage(ProxyMonitorPanel.class, "ProxyMonitorPanel.proxyToHostTextField.text")); // NOI18N
        proxyToHostTextField.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                proxyToHostTextFieldInputMethodTextChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel1)
                        .addComponent(portSpinner)
                        .addComponent(jLabel2)
                        .addComponent(jLabel3)
                        .addComponent(proxyToPortSpinner))
                    .addComponent(proxyToHostTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 323, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(298, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(portSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(proxyToPortSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(proxyToHostTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(62, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void portSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_portSpinnerStateChanged
        controller.changed();
    }//GEN-LAST:event_portSpinnerStateChanged

    private void proxyToPortSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_proxyToPortSpinnerStateChanged
        controller.changed();
    }//GEN-LAST:event_proxyToPortSpinnerStateChanged

    private void proxyToHostTextFieldInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_proxyToHostTextFieldInputMethodTextChanged
        controller.changed();
    }//GEN-LAST:event_proxyToHostTextFieldInputMethodTextChanged

    void load() {
        ProxySettingsManager settingsManager = Lookup.getDefault().lookup(ProxySettingsManager.class);
        ProxySettings proxySettings = settingsManager.getProxySettings();
        portSpinner.setValue(proxySettings.getProxyPort());
        proxyToPortSpinner.setValue(proxySettings.getProxyToPort());
        proxyToHostTextField.setText(proxySettings.getProxyToHost());
    }

    void store() {
        ProxySettingsManager settingsManager = Lookup.getDefault().lookup(ProxySettingsManager.class);
        ProxySettings proxySettings = new ProxySettings((Integer) portSpinner.getValue(), (Integer) proxyToPortSpinner.getValue(), proxyToHostTextField.getText());
        settingsManager.saveProxySettings(proxySettings);
    }

    boolean valid() {
        
        return true;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JSpinner portSpinner;
    private javax.swing.JTextField proxyToHostTextField;
    private javax.swing.JSpinner proxyToPortSpinner;
    // End of variables declaration//GEN-END:variables
}
